package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.UriUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RingAdapter;
import flc.ast.bean.MyRingBean;
import flc.ast.databinding.ActivityRingBinding;
import flc.ast.utils.AudioPlayerImpl;
import flc.ast.utils.Content;

public class RingActivity extends BaseAc<ActivityRingBinding> {

    public static String selName;
    private RingAdapter ringAdapter;
    private List<MyRingBean> listRing = new ArrayList<>();
    private int selPosition;
    private AudioPlayerImpl mAudioPlayer;

    @Override
    protected int onCreate() {
        return R.layout.activity_ring;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRingBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSetting.setOnClickListener(this);

        mDataBinding.rvRingList.setLayoutManager(new LinearLayoutManager(mContext));
        ringAdapter = new RingAdapter();
        mDataBinding.rvRingList.setAdapter(ringAdapter);
        ringAdapter.setOnItemClickListener(this);

        mAudioPlayer = new AudioPlayerImpl();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSetting://设定
                Intent intent = new Intent();
                intent.putExtra(Content.RING_DATA, (Parcelable) listRing.get(selPosition));
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        ringAdapter.getItem(selPosition).setSelected(false);
        selPosition = position;
        ringAdapter.getItem(position).setSelected(true);
        ringAdapter.notifyDataSetChanged();
        // 倒计时结束
        Uri uri = Uri.parse("android.resource://" + AppUtils.getAppPackageName() + "/" + ringAdapter.getItem(position).getMusic());
        mAudioPlayer.play(UriUtils.uri2File(uri).getPath());
    }

    @Override
    protected void initData() {
        //铃声
        listRing = new ArrayList<>();
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 1, R.raw.music1));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 2, R.raw.music2));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 3, R.raw.music3));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 4, R.raw.music4));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 5, R.raw.music5));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 6, R.raw.music6));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 7, R.raw.music7));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 8, R.raw.music8));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 9, R.raw.music9));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 10, R.raw.music10));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 11, R.raw.music11));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 12, R.raw.music12));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 13, R.raw.music13));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 14, R.raw.music14));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 15, R.raw.music15));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 16, R.raw.music16));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 17, R.raw.music17));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 18, R.raw.music18));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 19, R.raw.music19));
        listRing.add(new MyRingBean(getString(R.string.ring_text) + 20, R.raw.music20));

        for (int i = 0; i < listRing.size(); i++) {
            if (listRing.get(i).getName().equals(selName)) {
                listRing.get(i).setSelected(true);
                selPosition = i;
            }
        }

        ringAdapter.setList(listRing);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mAudioPlayer.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAudioPlayer.stop();
    }
}